package com.example.kermit.zhpaper.ui.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.kermit.zhpaper.R;
import com.example.kermit.zhpaper.config.MyAppConfig;
import com.example.kermit.zhpaper.entity.ItemInfo;
import com.example.kermit.zhpaper.http.MySingleRequestQueue;
import com.example.kermit.zhpaper.ui.adapter.MyRecyclerViewAdapter;
import com.google.gson.Gson;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Kermit on 15-7-28.
 * e-mail : wk19951231@163.com
 */
public class LatestFragment extends Fragment {

    private static final String LOG_TAG = "LatestFragment";


    @Bind(R.id.id_recyclerview)
    RecyclerView recyclerView;
    @Bind(R.id.id_swipe_refresh)
    SwipeRefreshLayout refreshLayout;


    MySingleRequestQueue requestQueue;
    MyRecyclerViewAdapter adapter;
    ItemInfo itemInfo;
    List<ItemInfo.Story> stories;

    LatestFragmentListener listener;

    public interface LatestFragmentListener{
        void LatestFragmentCallback(int newsId);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listener = (LatestFragmentListener) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestQueue = MySingleRequestQueue.getInstance(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.latest_fragment_layout, container, false);
        ButterKnife.bind(this, view);
        adapter = new MyRecyclerViewAdapter(getActivity());

        fetchData();

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                adapter.getData().clear();
                fetchData();
                adapter.notifyDataSetChanged();
                refreshLayout.setRefreshing(false);
            }

        });

        return view;
    }

    public void fetchData(){
        refreshLayout.setRefreshing(true);
        StringRequest latestNewsRequest = new StringRequest(MyAppConfig.LATEST_INFO,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(@NonNull String response) {
                        Log.i(LOG_TAG, String.valueOf(response));
                        Gson gson = new Gson();
                        itemInfo = gson.fromJson(response, ItemInfo.class);
                        stories = itemInfo.getStories();
                        adapter.setData(stories);
                        recyclerView.setAdapter(adapter);
                        setRecyclerViewListener(adapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i(LOG_TAG, error.toString());
                    }
                }
        );
        requestQueue.addtoRequestQueue(latestNewsRequest);
        refreshLayout.setRefreshing(false);
    }

    public void setRecyclerViewListener(MyRecyclerViewAdapter adapter) {

        adapter.setClickListener(new MyRecyclerViewAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position, int newsId) {
                listener.LatestFragmentCallback(newsId);
            }
        });

        adapter.setLongClickListener(new MyRecyclerViewAdapter.MyItemLongClickListener() {

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }
}
